<template>
  <div class="container">
    <div class="chatting_con" id="chatting_con">
      <div class="chatting_show" ref="dialogBox">
        <div
            :class="[i.type == 'response' ? 'atalk' : 'btalk']"
            v-for="i in talkList"
        >
          <img
              :class="[i.type == 'response' ? 'aicon' : 'bicon']"
              :src="[
              i.type == 'response'
                ? 'https://junweispicbed.oss-cn-guangzhou.aliyuncs.com/img/image-20221109004353193.png'
                : 'https://junweispicbed.oss-cn-guangzhou.aliyuncs.com/img/image-20221109004519413.png',
            ]"
          />
          <p>{{ i.word }}</p>
        </div>
      </div>
      <div class="chatting_input">
        <input
            type="text"
            class="chatting_word"
            v-model="query"
            @keyup.enter="submit"
        />
        <input
            type="button"
            value="发送"
            class="chatting_sub"
            @click="submit"
        />
      </div>
    </div>
    <div class="chatting_tips">
      <p>或许您想问这些？</p>
      <a
          v-for="i in tipList"
          href="javascript:void(0)"
          @click="ShowTips($event)"
      >{{ i }}</a
      >
    </div>
  </div>
</template>

<script>
import {chatting} from "@/api/api";

export default {
  data() {
    return {
      query: "",
      talkList: [{type: "response", word: "您好,请问有什么可以帮到您的?"}],
      tipList: [
        // "根腐病是什么？",
        // "如何预防根腐病？",
        // "阿维菌素的用法",
        // "蚜虫应该用什么药？",
        // "霜霉病有什么办法治？",
        // "多菌灵可以和吡虫啉一起用吗？",
        "什么是小番茄？",
        "小番茄如何防治红蜘蛛？",
        "小番茄蛀心虫如何防治？",
        "柚子溃疡病的防治技术？",
        "柚子溃疡病的发生症状与规律？",
        "柚子疮痂病的防治技术？",
        "柚子疮痂病的发生症状与规律？",
        "什么是沉香？",
        "沉香树如何防治天牛？",
        "沉香卷叶虫如何防治？",
      ],
    };
  },
  methods: {
    submit() {
      if (this.query.replace(/(^\s*)|(\s*$)/g, "") === "") {
        alert("消息不能为空");
        return;
      }
      this.talkList.push({type: "request", word: this.query});
      this.doPost(this.query);
      this.query = "";
      this.$nextTick(() => {
        this.$refs.dialogBox.scrollTop = this.$refs.dialogBox.scrollHeight;
      });
    },
    doPost(query) {
      chatting({text: query}).then((response) => {
        this.talkList.push({type: "response", word: response.data});
        this.$nextTick(() => {
          this.$refs.dialogBox.scrollTop = this.$refs.dialogBox.scrollHeight;
        });
      });
    },
    ShowTips(e) {
      this.query = e.target.innerText;
      this.submit();
    },
  },
};
</script>

<style scoped>
body {
  margin: 0;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
  height: 100vh;
}

.container {
  width: 100%;
  height: 85vh;
  min-width: 870px;
  min-height: 640px;
  margin: 0px;
  background: rgb(232, 232, 238) url("../assets/bg.svg") no-repeat fixed center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.chatting_con {
  position: absolute;
  width: 850px;
  height: 600px;
  border: 1px solid #666;
  border-radius: 12px;
  box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.25);
  background: rgba(255, 255, 255, 0.068);
  backdrop-filter: blur(5px);
  margin: 5px;
  padding: 10px 0 10px 10px;
}

.chatting_tips {
  position: absolute;
  width: 250px;
  height: 450px;
  border: 1px solid #666;
  border-radius: 12px;
  left: calc(100% - 300px);
  padding: 5px;
  background-color: rgba(255, 255, 255, 0.301);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
}

.chatting_show {
  width: 830px;
  height: 540px;
  margin: 5px;
  overflow: auto;
  overflow-wrap: break-word;
}

.chatting_show::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 10px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 1px;
}

.chatting_show::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  /* -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2); */
  background: rgba(189, 181, 181, 0.5);
}

.chatting_show::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  /* -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2); */
  border-radius: 10px;
  /* background: rgba(255, 255, 255, 0.25);    */
}

.chatting_input {
  height: 45px;
  width: 830px;
  margin-right: 10px;
  margin-left: 5px;
}

.chatting_word {
  width: calc(100% - 65px);
  height: 40px;
  margin: 2px 0 2px 0;
  font-size: 15px;
  outline: none;
  text-indent: 10px;
  border: 1px solid #666;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.4);
  color: rgba(8, 8, 8, 0.76);
  z-index: -1;
  position: inherit;
}

.chatting_sub {
  width: 50px;
  height: 40px;
  font-size: 15px;
  outline: none;
  margin: 2px 0 2px 10px;
  border: 1px solid #666;
  border-radius: 8px;
  color: rgba(8, 8, 8, 0.76);
  background: rgba(255, 255, 255, 0.5);
}

.atalk {
  margin: 12px 0 0 5px;
}

.btalk {
  margin: 12px 5px 0 0;
  text-align: right;
  display: block;
}

.chatting_tips a {
  display: inline-block;
  position: relative;
  word-wrap: break-word;
  word-break: break-all;
  padding-left: 5px;
  color: #000000;
  margin: 5px;
  overflow: hidden;
  text-decoration: none;
}

.chatting_tips a:hover {
  background: #ffffffc5;
  border: 1px solid;
  border-radius: 8px;
  padding: 3px;
  transform: translateY(-5px);
  transition: 0.5s all;
}

.chatting_tips p {
  text-align: center;
  padding: 10px;
  align-content: center;
  font-weight: bold;
}

.atalk p {
  display: inline-block;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 10px;
  max-width: 260px;
  color: rgba(8, 8, 8, 0.76);
  padding: 5px 10px;
  margin: 3px 0 0 5px;
  border: 1px solid #666;
  position: relative;
}

.btalk p {
  display: inline-block;
  background: #cceeff;
  border-radius: 10px;
  max-width: 260px;
  color: rgba(8, 8, 8, 0.76);
  border: 1px solid #666;
  padding: 5px 10px;
  margin: 5px 5px 0 0;
  position: relative;
  text-align: left;
  word-wrap: break-word;
  /* word-break: normal; */
  /* white-space: pre-wrap; */
  /* overflow: hidden; */
}

hr {
  height: 30px;
  border-style: solid;
  border-color: #8c8b8b;
  border-width: 1px 0 0 0;
  border-radius: 20px;
}

hr::before {
  display: block;
  content: "";
  height: 30px;
  margin-top: -31px;
  border-style: solid;
  border-color: #8c8b8b;
  border-width: 0 0 1px 0;
  border-radius: 20px;
}

.aicon {
  float: left;
  height: 38px;
  border-radius: 18px;
}

.bicon {
  float: right;
  height: 38px;
  border-radius: 18px;
}
</style>
